/* ==============================================================================
 * 创 建 者：lyt
 * 创建日期：2017-07-14	
 * 功能描述：

 * 修 改 人:
 * 修改时间:
 * 修改描述：
 * ==============================================================================*/
//汇款
var SubmitUrl = baseUrl+'FinanceF/SubmitRemittance';
//获取汇款配置
var GetSetUrl =  baseUrl+'CollectionF/GetCollectionAddress';
//会员信息获取
var getMemberViewUrl = baseUrl + 'MemberF/MemberView';
//获取记录
var getListUrl = baseUrl+'FinanceF/GetRemittances';
//获取会员名称
var GetMemberNameUrl = baseUrl+'DefaultF/GetMemberName';


var vu;
var mescroll,clipboard;
$(function(){
	initVue();
});

function initVue(){
	vu = new Vue({
		el : '#content',
		data : {
			member : '',
			SetCoins : [],//转账币种
			ShowCoins : [],//显示币种
			chooseCoinIndex : 0,//选择币种
			
			selectedCoin : {//选择币种
				CoinOid : '',
				RemittanceMax : '',//最大值
				RemittanceMin : '',//最小值
				Radix : '',//基数
				Rate : 0,//汇率
			},
			Count : '',
			RemitDate : '',
			Note : '',
			
			pageIndex : 1,
			TotalPageCount : '',
			List : [],
			banks : [],
			bankIndex : 0,
			checkedBank : {
				BankCode : '',
				BankName : '',
				CardName : '',
				CardNumber : '',
				SubBranchBank : '',
				Telphone : '',
			},
			BankName:'',
			Voucher : '',
			
			Eth_Addr_QrImg:'',
			EthAddress:"",
		},
		mounted: function() {
			this.GetSet();
//			this.getMemberView();
//			initMescroll();
		},
		methods : {
			savePic:savePic,
			getCopy:getCopy,
			GetSet : GetSet,
			Submit : Submit,
			getMemberView : getMemberView,
			chooseBank :chooseBank,
			chooseCoin : chooseCoin,
		}
	})
}

//保存图片到手机
function savePic(){         
    var picurl= $("#code img").attr("src");
    //alert(picurl);
    savePicture(picurl);
}

var triggerEvent = "touchstart";
function savePicture(Url){
    var blob=new Blob([''], {type:'application/octet-stream'});
    var url = URL.createObjectURL(blob);
    var a = document.createElement('a');
    a.href = Url;
    a.download = Url.replace(/(.*\/)*([^.]+.*)/ig,"$2").split("?")[0];
    var e = document.createEvent('MouseEvents');
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
    URL.revokeObjectURL(url);
}
function getCopy(){
	clipboard = new ClipboardJS('.copy',{
		text: function() {
			return vu.EthAddress;
        }
	});
	clipboard.on('success', function(e) {
        mui.toast('复制成功！');
    });

    clipboard.on('error', function(e) {
        mui.toast('复制失败！');
    });
}

function chooseCoin(){
	vu.selectedCoin = vu.SetCoins[vu.chooseCoinIndex];
}

function chooseBank(){
	vu.checkedBank = vu.banks[vu.bankIndex];
}

function getList(){
	common.get(getListUrl,{pageIndex: vu.pageIndex,pageSize: 10,},'json',function(r){
		if(r.ErrorCode==0){
			vu.TotalPageCount = r.Data.TotalPageCount;
			mescroll.endByPage(r.Data.TotalItemCount, vu.TotalPageCount);
			if(vu.pageIndex == 1){
				vu.List = r.Data.List;
			}else{
				vu.List = vu.List.concat(r.Data.List);
			}
			vu.pageIndex++;
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function initMescroll(){
	mescroll = new MeScroll("item2", {
		down:{
			use: false, //是否初始化下拉刷新; 默认true
			callback: function(mescroll) {
				vu.pageIndex = 1;
				mescroll.resetUpScroll();
				getList();
			}
		},
		up: {
			auto: true,
			isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): http://www.mescroll.com/qa.html#q10
			clearEmptyId: "dataList", //1.下拉刷新时会自动先清空此列表,再加入数据; 2.无任何数据时会在此列表自动提示空
			toTop:{ //配置回到顶部按钮
				src : "phoneContent/img/top.png", //默认滚动到1000px显示,可配置offset修改
				//offset : 1000
			},
			htmlNodata: '<p class="upwarp-nodata">-- 没有更多数据了 --</p>', //无数据的布局
			callback: function (){//上拉回调
				getList();
			},
			page : {
				size : 10, 
			},
		}
	})
}

function Submit(){
	if(!vu.selectedCoin.CoinOid){
		mui.toast("请选择币种");
		return;
	}
	if(!vu.Count){
		mui.toast("请输入汇款金额");
		return;
	}
	if(!vu.Voucher){
		mui.toast("请上传汇款凭证");
		return;
	}
	if(!vu.Note){
		mui.toast("请填写打款说明");
		return;
	}
	if(!vu.RemitDate){
		mui.toast("请选择打款时间");
		return;
	}
	var btnArray = ['取消', '确定'];
	mui.prompt('', '', '请输入安全密码：', btnArray, function(e) {
		if(!e.value){
			mui.toast('请输入安全密码！');
			return;
		}
        if (e.index == 1) {
            common.post(SubmitUrl,{CoinOID : vu.selectedCoin.CoinOid,BankName : vu.checkedBank.BankName,Count : vu.Count,Voucher : vu.Voucher,RemitDate : vu.RemitDate,Note : vu.Note, SecondPwd : e.value},'json',function(r){
				if(r.ErrorCode==0){
					mui.toast(r.ErrorMsg);
					vu.Voucher = '';
					vu.Count = '';
					vu.pageIndex = 1;
					mescroll.resetUpScroll();
					getList();
					mui.trigger($('.mui-control-item').eq(1)[0],'touchstart');
					mui.trigger($('.mui-control-item').eq(1)[0],'tap')
				}else{
					mui.toast(r.ErrorMsg);
				}
			})
        } 
    },'div')
	document.querySelector('.mui-popup-input input').type='password';
}

function GetSet(){
	common.get(GetSetUrl,{},'json',function(r){
		if(r.ErrorCode==0){
			vu.EthAddress = r.Data.Eth_Addr;
			vu.Eth_Addr_QrImg = r.Data.Eth_Addr_QrImg;
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function getMemberView(){
	common.get(getMemberViewUrl,{},'json',function(r){
		if(r.ErrorCode==0){
			vu.member = r.Data.member;
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

//上传图片
function doUpload2() {
	var formData = new FormData();
	if($(".upload2")[0].files.length == 0) {
		mui.toast("请重新选择图片！");
		return false;
	};
	formData.append("file", $(".upload2")[0].files[0]);
	formData.append("picTypeName", "USER_HEAD");
	$.ajax({
		url: baseUrl + "common/uploadimg",
		type: "POST",
		data: formData,
		async: false,
		cache: false,
		contentType: false,
		processData: false,
		success: function(r) {
			r = JSON.parse(r);
			var u = r.Data.url;
			if(r.ErrorCode == 0){
				mui.toast("上传成功！");
				vu.Voucher = u;
			}else{
				mui.toast(r.ErrorMsg);
			}
		},
		error: function(returndata) {
			mui.toast("操作失败");
		}
	});
};
